package com.bank.abmock.mapper;

import com.bank.abmock.config.RouteConfig;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.LinkedList;
import java.util.List;

@Component
public class RouteMapperCenter{
    List<RouteConfig> routeConfigs;
    @PostConstruct
    public void init() {
        routeConfigs = new LinkedList<>();
    }

    public RouteConfig map(String url, Object request) {
        if(url == null){
            return null;
        }
        for(RouteConfig routeConfig : routeConfigs){
            if(url.equals(routeConfig.getUrl())){
                return routeConfig;
            }
        }
        return null;
    }

    public Object process(String url, Object request){
        RouteConfig routeConfig = map(url, request);
        if(routeConfig != null){
            return routeConfig.process(request);
        }
        return null;
    }
    /*
     * 注册路由配置
     * */
    public void register(List<RouteConfig> routeConfigList){
        routeConfigs.addAll(routeConfigList);
    }


}
